package com.techthinker.dreamBuilder.Setting;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.techthinker.dreamBuilder.Database.DatabaseHelper;
import com.techthinker.dreamBuilder.Dictionary.DictionaryDAL;
import com.techthinker.dreamBuilder.Entity.Task;
import com.techthinker.dreamBuilder.Define.Constant;

public class TaskSettingDAL {
	
	private static final String KEY_DIC_ID = "dictionary_id";
	private static final String KEY_TOTALE_DAYS = "totalDays";
	private static final String KEY_START_DATE = "startDate";
	private static final String KEY_LISTS_DAY = "listsPerDay";
	private static final String KEY_WORDS_GROUP = "wordsPerGroup";
	private static final String KEY_GROUP_ROUND = "groupPerRound";
	private static final String KEY_SORT = "sort";
	private static final String KEY_ROW_ID = "task_id";
	
	private final Context mCtx;
	
	//
	private DatabaseHelper mDbHelper = null;
	private SQLiteDatabase mDb = null ;
	
	public TaskSettingDAL(Context ctx){
		this.mCtx = ctx;
	}
	
	public TaskSettingDAL Open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx, Constant.DB_NAME);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}
	
	public void Close(){
		this.mDbHelper.close();
		this.mDbHelper = null;
		this.mDb = null;
	}
	
	public boolean isClosed(){
		if(this.mDbHelper == null )
			return true;
		else
			return false;
	}
	
	//create a task item
	public long createTask(Task task){
		ContentValues cv = new ContentValues();
		cv.put(this.KEY_DIC_ID, task.getDictionary().getDictionary_id());
		cv.put(this.KEY_TOTALE_DAYS , task.getTotalDays());
		cv.put(this.KEY_START_DATE, task.getStartDate().toString());
		cv.put(this.KEY_LISTS_DAY , task.getListsPerDay());
		cv.put(this.KEY_WORDS_GROUP , task.getWordsPerDay());
		cv.put(this.KEY_GROUP_ROUND, task.getGroupsPerDay());
		cv.put(this.KEY_SORT, task.getSort());
		return this.mDb.insert(Constant.TABLE_TASK,null,cv);
	}
	
	// update a task item
	public boolean updateTask(Task task) {
		ContentValues cv = new ContentValues();
		cv.put(this.KEY_DIC_ID, task.getDictionary().getDictionary_id());
		cv.put(this.KEY_TOTALE_DAYS , task.getTotalDays());
		cv.put(this.KEY_START_DATE, task.getStartDate().toString());
		cv.put(this.KEY_LISTS_DAY , task.getListsPerDay());
		cv.put(this.KEY_WORDS_GROUP , task.getWordsPerDay());
		cv.put(this.KEY_GROUP_ROUND, task.getGroupsPerDay());
		cv.put(this.KEY_SORT, task.getSort());
		
		return mDb.update(Constant.TABLE_TASK, cv,
				KEY_ROW_ID + "=" + task.getTask_id(), null) > 0;
	}
	
	// delete a task item
	public boolean deleteTask(Task task) {
		return mDb.delete(Constant.TABLE_TASK,
				KEY_ROW_ID + "=" + task.getTask_id(), null) > 0;
	}
	
	//
	public boolean deleteTasks(int task_id) {
		return mDb.delete(Constant.TABLE_TASK, KEY_ROW_ID + "=" + task_id, null) > 0;
	}
		
	//retrieve all tasks
	public List<Task> fetchAllTasks(){
		List<Task> lists = new ArrayList<Task>();
		
		Cursor cursor = this.mDb.query( Constant.TABLE_TASK, new String[]{
				KEY_DIC_ID,KEY_TOTALE_DAYS,KEY_START_DATE,KEY_LISTS_DAY,
				KEY_WORDS_GROUP,KEY_GROUP_ROUND,KEY_SORT}, 
				null, null, null, null, null);
		
		Task task = new Task();
		
		DictionaryDAL dicDAL = new DictionaryDAL(mCtx);
		dicDAL.open();
		while(cursor.moveToNext()){
			task.setTask_id(cursor.getInt(cursor.getColumnIndex(KEY_ROW_ID)));
			task.setDictionary(dicDAL.fetchDictionaryById(cursor
					.getInt(cursor.getColumnIndex(KEY_DIC_ID))));			
			
			task.setTotalDays(cursor.getInt(cursor.getColumnIndex(KEY_TOTALE_DAYS)));
			task.setStartDate(Date.valueOf(cursor.getString(cursor
					.getColumnIndex(KEY_START_DATE))));
			task.setListsPerDay(cursor.getInt(cursor
					.getColumnIndex(KEY_LISTS_DAY)));
			task.setWordsPerDay(cursor.getInt(cursor
					.getColumnIndex(KEY_WORDS_GROUP)));
			task.setGroupsPerDay(cursor.getInt(cursor
					.getColumnIndex(KEY_GROUP_ROUND)));
			task.setSort(cursor.getInt(cursor.getColumnIndex(KEY_SORT)));
			
			lists.add(task);
		}
		dicDAL.close();
		cursor.close();
		return lists;
	}
	
	// retrieve all lists to cursor
	public Cursor fetchAllTasksToCursor() {
		return mDb.query(Constant.TABLE_TASK, new String[] {
				KEY_DIC_ID,KEY_TOTALE_DAYS,KEY_START_DATE,KEY_LISTS_DAY,
				KEY_WORDS_GROUP,KEY_GROUP_ROUND,KEY_SORT}, 
				null, null, null, null, null);
	}
	
	//retrieve a task item by id
	public Task fetchTaskById(long rowId){
		
		Cursor cursor = this.mDb.query(true, Constant.TABLE_TASK, new String[]{
				KEY_DIC_ID,KEY_TOTALE_DAYS,KEY_START_DATE,KEY_LISTS_DAY,
				KEY_WORDS_GROUP,KEY_GROUP_ROUND,KEY_SORT}, 
				this.KEY_ROW_ID + rowId,null, null, null, null, null);
		
		Task task = new Task();
		
		DictionaryDAL dicDAL = new DictionaryDAL(mCtx);
		dicDAL.open();
		
		if (cursor.moveToFirst()){
			
			task.setTask_id(cursor.getInt(cursor.getColumnIndex(KEY_ROW_ID)));
			task.setDictionary(dicDAL.fetchDictionaryById(cursor
					.getInt(cursor.getColumnIndex(KEY_DIC_ID))));
			dicDAL.close();
			
			task.setTotalDays(cursor.getInt(cursor.getColumnIndex(KEY_TOTALE_DAYS)));
			task.setStartDate(Date.valueOf(cursor.getString(cursor
					.getColumnIndex(KEY_START_DATE))));
			task.setListsPerDay(cursor.getInt(cursor
					.getColumnIndex(KEY_LISTS_DAY)));
			task.setWordsPerDay(cursor.getInt(cursor
					.getColumnIndex(KEY_WORDS_GROUP)));
			task.setGroupsPerDay(cursor.getInt(cursor
					.getColumnIndex(KEY_GROUP_ROUND)));
			task.setSort(cursor.getInt(cursor.getColumnIndex(KEY_SORT)));
			
			cursor.close();
			return task;
		}
		cursor.close();
		return null;
		
	}

}
